#include "thread.h"
#include "main.h"

Thread::Thread( MainFrame* pFrame )  
: wxThread( wxTHREAD_DETACHED )
{
	m_pcFrame = pFrame;
}

Thread::~Thread()
{
	wxCriticalSectionLocker enter( m_pcFrame->m_pThreadCS );

	// the thread is being destroyed; make sure not to leave dangling pointers around
	m_pcFrame->GetThread() = NULL;
}

wxThread::ExitCode Thread::Entry()
{
	int sleepTime = 0;
	
	// to make thread deletable
	while( !TestDestroy() )
	{
		sleepTime++;
		wxThread::This()->Sleep( 1 );		
		
		if( !TestDestroy() && sleepTime == m_pcFrame->GetSleepPeriod() )
		{
			wxCommandEvent event( wxEVT_THREAD );

			// send in a thread-safe way
			wxPostEvent( m_pcFrame, event );
			
			break;
		}			
	}
	
	return (wxThread::ExitCode)0;     // success
}